package com.wyp168.staservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wyp168.commonutils.R;
import com.wyp168.staservice.client.UCenterClient;
import com.wyp168.staservice.entity.StatisticsDaily;
import com.wyp168.staservice.mapper.StatisticsDailyMapper;
import com.wyp168.staservice.service.StatisticsDailyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author wyp168
 * @since 2022-06-04
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {

    @Qualifier("com.wyp168.staservice.client.UCenterClient")
    @Autowired
    private UCenterClient uCenterClient;

    //统计某一天注册的人数,生成统计数据
    @Override
    public Integer registerCount(String day) {
        //向统计表中添加记录之前，先判断统计表中有无该日期的记录，如果有，则先删除，避免重复添加
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.eq("date_calculated",day);
        baseMapper.delete(wrapper);
        R registerInfo = uCenterClient.countRegister(day);
        Integer count = (Integer) registerInfo.getData().get("registerCount");
        //把统计数据放到统计分析表中
        StatisticsDaily sta = new StatisticsDaily();
        sta.setRegisterNum(count);  //统计注册人数
        sta.setDateCalculated(day); //统计日期

        sta.setVideoViewNum(RandomUtils.nextInt(100,200));
        sta.setLoginNum(RandomUtils.nextInt(100,200));
        sta.setCourseNum(RandomUtils.nextInt(100,200));

        return count;
    }

    //根据需要获取数据类型，开始时间和结束时间来查询图表显示数据
    @Override
    public Map<String, Object> getShowDate(String type, String begin, String end) {
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.between("date_calculated",begin,end);
        //查询指定字段
        wrapper.select("date_calculated",type);

        List<StatisticsDaily> staList = baseMapper.selectList(wrapper);

        //封装数据
        //因为返回的是两部分数组： 日期 和 日期对应的数据
        //前端要求数组json结构，对应后端java嗲吗是list集合
        //创建两个list：日期list，日期对应数据list
        List<String> dateCalculatedList = new ArrayList<>();
        List<Integer> numDataList = new ArrayList<>();

        //遍历查询所有数据list集合，进行封装
        for (StatisticsDaily daily : staList) {
            //封装日期
            dateCalculatedList.add(daily.getDateCalculated());
            //封装对应数量
            switch (type) {
                case "login_num":
                    numDataList.add(daily.getLoginNum());
                    break;
                case "register_num":
                    numDataList.add(daily.getRegisterNum());
                    break;
                case "video_view_num":
                    numDataList.add(daily.getVideoViewNum());
                    break;
                case "course_num":
                    numDataList.add(daily.getCourseNum());
                    break;
                default:
                    break;
            }
        }

        Map<String,Object> map = new HashMap<>();
        map.put("dateCalculatedList",dateCalculatedList);
        map.put("numDataList",numDataList);

        return map;
    }
}
